package com.dianping.cat.home.dependency.config.transform;

import com.dianping.cat.home.dependency.config.IEntity;
import com.dianping.cat.home.dependency.config.IVisitor;
import com.dianping.cat.home.dependency.config.entity.DomainConfig;
import com.dianping.cat.home.dependency.config.entity.EdgeConfig;
import com.dianping.cat.home.dependency.config.entity.NodeConfig;
import com.dianping.cat.home.dependency.config.entity.TopologyGraphConfig;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
























public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  44 */   public DefaultXmlBuilder() { this(false); }



/*  48 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  52 */     this.m_compact = compact;
/*  53 */     this.m_sb = sb;
/*  54 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  58 */     entity.accept(this.m_visitor);
/*  59 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  63 */     this.m_level--;

/*  65 */     indent();
/*  66 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  70 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  74 */     if (value == null) {
/*  75 */       return null;
    }

    String str = toString(value);
/*  79 */     int len = str.length();
/*  80 */     StringBuilder sb = new StringBuilder(len + 16);

/*  82 */     for (int i = 0; i < len; i++) {
/*  83 */       char ch = str.charAt(i);

/*  85 */       switch (ch) {
        case '<':
/*  87 */           sb.append("&lt;");
          break;
        case '>':
/*  90 */           sb.append("&gt;");
          break;
        case '&':
/*  93 */           sb.append("&amp;");
          break;
        case '"':
/*  96 */           if (!text) {
/*  97 */             sb.append("&quot;");
            break;
          }
        default:
/* 101 */           sb.append(ch);
          break;
      }

    }
/* 106 */     return sb.toString();
  }

  protected void indent() {
/* 110 */     if (!this.m_compact) {
/* 111 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 112 */         this.m_sb.append("   ");
      }
    }
  }


/* 118 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 122 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 126 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 130 */     indent();

/* 132 */     this.m_sb.append('<').append(name);

/* 134 */     int len = nameValues.length;

/* 136 */     for (int i = 0; i + 1 < len; i += 2) {
/* 137 */       Object attrName = nameValues[i];
/* 138 */       Object attrValue = nameValues[i + 1];

/* 140 */       if (attrValue != null) {
/* 141 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 145 */     if (dynamicAttributes != null) {
/* 146 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 147 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 151 */     if (text != null && closed) {
/* 152 */       this.m_sb.append('>');
/* 153 */       this.m_sb.append(escape(text, true));
/* 154 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 156 */       if (closed) {
/* 157 */         this.m_sb.append('/');
      } else {
/* 159 */         this.m_level++;
      }

/* 162 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 168 */     if (value instanceof String)
/* 169 */       return (String)value; 
/* 170 */     if (value instanceof Collection) {
/* 171 */       Collection<Object> list = (Collection)value;
/* 172 */       StringBuilder sb = new StringBuilder(32);
/* 173 */       boolean first = true;

/* 175 */       for (Object item : list) {
/* 176 */         if (first) {
/* 177 */           first = false;
        } else {
/* 179 */           sb.append(',');
        }

/* 182 */         if (item != null) {
/* 183 */           sb.append(item);
        }
      }

/* 187 */       return sb.toString();
/* 188 */     }  if (value.getClass().isArray()) {
/* 189 */       int len = Array.getLength(value);
/* 190 */       StringBuilder sb = new StringBuilder(32);
/* 191 */       boolean first = true;

/* 193 */       for (int i = 0; i < len; i++) {
/* 194 */         Object item = Array.get(value, i);

/* 196 */         if (first) {
/* 197 */           first = false;
        } else {
/* 199 */           sb.append(',');
        }

/* 202 */         if (item != null) {
/* 203 */           sb.append(item);
        }
      }

/* 207 */       return sb.toString();
    }

/* 210 */     return String.valueOf(value);
  }



/* 215 */   public void visitDomainConfig(DomainConfig domainConfig) { startTag("domain-config", true, null, new Object[] { "id", domainConfig.getId(), "warning-threshold", Integer.valueOf(domainConfig.getWarningThreshold()), "error-threshold", Integer.valueOf(domainConfig.getErrorThreshold()), "warning-response-time", Double.valueOf(domainConfig.getWarningResponseTime()), "error-response-time", Double.valueOf(domainConfig.getErrorResponseTime()), "min-count-threshold", Integer.valueOf(domainConfig.getMinCountThreshold()) }); }




/* 220 */   public void visitEdgeConfig(EdgeConfig edgeConfig) { startTag("edge-config", true, null, new Object[] { "key", edgeConfig.getKey(), "type", edgeConfig.getType(), "from", edgeConfig.getFrom(), "to", edgeConfig.getTo(), "warning-threshold", Integer.valueOf(edgeConfig.getWarningThreshold()), "error-threshold", Integer.valueOf(edgeConfig.getErrorThreshold()), "warning-response-time", Double.valueOf(edgeConfig.getWarningResponseTime()), "error-response-time", Double.valueOf(edgeConfig.getErrorResponseTime()), "min-count-threshold", Integer.valueOf(edgeConfig.getMinCountThreshold()) }); }



  public void visitNodeConfig(NodeConfig nodeConfig) {
/* 225 */     startTag("node-config", null, new Object[] { "type", nodeConfig.getType(), "default-warning-threshold", nodeConfig.getDefaultWarningThreshold(), "default-error-threshold", nodeConfig.getDefaultErrorThreshold(), "default-warning-response-time", nodeConfig.getDefaultWarningResponseTime(), "default-error-response-time", nodeConfig.getDefaultErrorResponseTime(), "default-min-count-threshold", Integer.valueOf(nodeConfig.getDefaultMinCountThreshold()) });

/* 227 */     if (!nodeConfig.getDomainConfigs().isEmpty()) {
/* 228 */       for (DomainConfig domainConfig : nodeConfig.getDomainConfigs().values()) {
/* 229 */         domainConfig.accept(this.m_visitor);
      }
    }

/* 233 */     endTag("node-config");
  }


  public void visitTopologyGraphConfig(TopologyGraphConfig topologyGraphConfig) {
/* 238 */     startTag("topology-graph-config", null, new Object[0]);

/* 240 */     if (!topologyGraphConfig.getNodeConfigs().isEmpty()) {
/* 241 */       for (NodeConfig nodeConfig : topologyGraphConfig.getNodeConfigs().values()) {
/* 242 */         nodeConfig.accept(this.m_visitor);
      }
    }

/* 246 */     if (!topologyGraphConfig.getEdgeConfigs().isEmpty()) {
/* 247 */       for (EdgeConfig edgeConfig : topologyGraphConfig.getEdgeConfigs().values()) {
/* 248 */         edgeConfig.accept(this.m_visitor);
      }
    }

/* 252 */     endTag("topology-graph-config");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/dependency/config/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */